<?php

namespace app\api;

use app\model\WorldResource;
use app\model\WorldUnit;
use support\Request;

class GetData
{
    public $data = [];

    public function getResource(Request $request)
    {
        // $data = WorldResource::query()->where('status', 1)->get();

        foreach(WorldResource::query()->where('status', 1)->get() as $k=>$item) {
            $this->data[$k]['id'] = $item->id;
            $this->data[$k]['name'] = $item->name;
            $this->data[$k]['init_value'] = $item->init_value;
            $this->data[$k]['value'] = $item->value;
            // $this->data[$k]['unit_id'] = WorldUnit::query()->where('id', $item->unit_id)->first()->unit;
            $this->data[$k]['unit_id'] = $item->unit->unit;
            $this->data[$k]['add'] = $item->add;
            $this->data[$k]['status'] = $item->status;
        }

        return jsonSuccess($this->data);
    }

    public function putData(Request $request)
    {
        $resource = WorldResource::query()->where('status', 1)->get();

        foreach($resource as $k=>$item) {
            $data[$k][$item->name] = $item->value;
        }

        return $data;
    }
}